package com.ayu.shortlink.settlement.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ayu.shortlink.settlement.common.enums.CouponStatusEnum;
import com.ayu.shortlink.settlement.dao.entity.UserCouponDO;
import com.ayu.shortlink.settlement.dao.mapper.UserCouponMapper;
import com.ayu.shortlink.settlement.dto.req.UserCouponPageQueryReqDTO;
import com.ayu.shortlink.settlement.dto.resp.UserCouponPageQueryRespDTO;
import com.ayu.shortlink.settlement.service.UserCouponQueryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UserCouponQueryServiceImpl extends ServiceImpl<UserCouponMapper,UserCouponDO> implements UserCouponQueryService {
    private final UserCouponMapper userCouponMapper;
    @Override
    public IPage<UserCouponPageQueryRespDTO> pageEffectiveUserCoupon(UserCouponPageQueryReqDTO requestParam) {
        LambdaQueryWrapper<UserCouponDO> queryWrapper = Wrappers.lambdaQuery(UserCouponDO.class)
                .eq(UserCouponDO::getUserId, requestParam.getUserId())
                .eq(UserCouponDO::getStatus, CouponStatusEnum.EFFECTIVE.getType());

        IPage<UserCouponDO> couponPage = userCouponMapper.selectPage(requestParam, queryWrapper);
        return couponPage.convert(each -> BeanUtil.toBean(each, UserCouponPageQueryRespDTO.class));
    }

    @Override
    public IPage<UserCouponPageQueryRespDTO> pageInvalidUserCoupon(UserCouponPageQueryReqDTO requestParam) {
        LambdaQueryWrapper<UserCouponDO> queryWrapper = Wrappers.lambdaQuery(UserCouponDO.class)
                .eq(UserCouponDO::getUserId, requestParam.getUserId())
                .eq(UserCouponDO::getStatus, CouponStatusEnum.ENDED.getType());

        IPage<UserCouponDO> couponPage = userCouponMapper.selectPage(requestParam, queryWrapper);
        return couponPage.convert(each -> BeanUtil.toBean(each, UserCouponPageQueryRespDTO.class));
    }
}
